package com.ng.stat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ng.common.log.entity.SerachLogEntity;
import com.ng.common.log.service.SearchLogService;
import com.ng.common.service.CacheServiceImpl;
import com.ng.common.utils.*;

import com.ng.stat.dao.SearchDao;
import com.ng.stat.entity.BookEntity;
import com.ng.stat.entity.BookSortEntity;
import com.ng.stat.service.BookClsService;
import com.ng.stat.service.SearchService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;


/**
 * @author cb
 * @Description TODO
 * @date 2023/2/26
 */

@Service
public class SearchServiceImp extends CacheServiceImpl<SearchDao, BookEntity> implements SearchService {

    @Autowired
    private BookClsService bookClsService;

    @Autowired
    private SearchLogService sysLogService;


    @Override
    public BookEntity selectonebyid(String id) {

        BookEntity bb=  this.baseMapper.selectById(id);
        return   bb;
    }

    @Override
    public int insert(BookEntity entity) {

        int a= this.baseMapper.insert(entity);
        return   a;
    }

    @Override
    public int update(BookEntity entity) {
        int a= this.baseMapper.updateById(entity);
        return   a;
    }

    @Override
    public int update(BookEntity entity, UpdateWrapper updateWrapper) {
        int a= this.baseMapper.update(entity,updateWrapper);
        return  a;
    }

    @Override
    public int getcout() {
        QueryWrapper<BookEntity> queryWrapper=new QueryWrapper<BookEntity>();
      return  this.baseMapper.selectCount(queryWrapper);
    }

    @Override
    public PageUtils queryPage(BookEntity entity, PageUtils page) {

        String bookname = entity.get书名();

        String  serchtype=entity.getSearchtype();
        String year=entity.get出版日期();
        List<String>syscode=entity.getSyscodes();
        List<String>yearlist=entity.getYearlist();

        QueryWrapper<BookEntity> queryWrapper=new QueryWrapper<BookEntity>();
        if(syscode!=null&&syscode.size()>0){
            queryWrapper.and(wrapper->{
                for (String item:syscode ) {
                    wrapper.or().likeRight("classcode",item);
                }
            });
        }
        if(yearlist!=null&&yearlist.size()>0){
            queryWrapper.and(wrapper->{
                for (String item:yearlist ) {
                    wrapper.or().eq("出版日期",item);
                }
            });
        }

      //  queryWrapper.like("classcode",1);
        queryWrapper.eq(StringUtils.isNotBlank(year),"出版日期", year);
        queryWrapper.eq("SYS_FLD_CHECK_STATE",1);
        queryWrapper.eq(StringUtils.isNotBlank(entity.get丛书名()),"丛书名",entity.get丛书名());
        queryWrapper.like(StringUtils.isNotBlank(bookname),serchtype, bookname);
       if(StringUtils.isNotBlank(bookname)&&bookname.length()>=2){
           SerachLogEntity sysLog= new SerachLogEntity();
           //获取request
           HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
           //设置IP地址
           sysLog.setIp(IPUtils.getIpAddr(request));

           Subject subject = SecurityUtils.getSubject();

           Object user = subject.getPrincipal();

           //用户名
           Object username = ReflectUtils.getFieldValue(user, "userNo");
           if(username != null)
               sysLog.setUser_name(username.toString());


           sysLog.setCreate_time(new Date());
           sysLog.setStr_query(bookname);
           sysLogService.save(sysLog);
       }

        queryWrapper.orderByDesc("downnum","previewnum","bb");
        //IPage<BookEntity> ipage = this.page(new Query<BookEntity>(page).getPage(),queryWrapper);
        IPage<BookEntity> ipage =this.baseMapper.queryPage1 (new Query<BookEntity>(page).getPage(),queryWrapper);
        for(int i=0;i<ipage.getRecords().size();i++){
            BookEntity bookEntity=ipage.getRecords().get(i);
            String   classcode=bookEntity.getClasscode();
            if(classcode.indexOf(",")>=0){
               String[] classcodes=classcode.split(",");
               String tempclassname="";
               for(int j=0;j<classcodes.length;j++){
                   BookSortEntity  tt=new  BookSortEntity();
                   tt.setCode(classcodes[j]);
                   tt=bookClsService.querybookclsbycode(tt);
                   if(tt!=null){
                   tempclassname+=tt.name+",";}
               }
               tempclassname=tempclassname.substring(0,tempclassname.lastIndexOf(","));
                bookEntity.setClassname(tempclassname);
            }
            else {
                BookSortEntity  tt=new  BookSortEntity();
                tt.setCode(classcode);
                tt=bookClsService.querybookclsbycode(tt);
                if(tt!=null) {
                    bookEntity.setClassname(tt.getName());
                }
            }
        }
        return new PageUtils(ipage);
    }

    @Override
    public int removeByIds(String[] idList) {

     return    this.baseMapper.updateStateByIDs(idList);


    }

    @Override
    public PageUtils gethotpage(BookEntity entity, PageUtils page) {
        String bookname = entity.get书名();

        String  serchtype=entity.getSearchtype();
        String year=entity.get出版日期();
        List<String>syscode=entity.getSyscodes();
        List<String>yearlist=entity.getYearlist();

        QueryWrapper<BookEntity> queryWrapper=new QueryWrapper<BookEntity>();
        if(syscode!=null&&syscode.size()>0){
            queryWrapper.and(wrapper->{
                for (String item:syscode ) {
                    wrapper.or().likeRight("classcode",item);
                }
            });
        }
        if(yearlist!=null&&yearlist.size()>0){
            queryWrapper.and(wrapper->{
                for (String item:yearlist ) {
                    wrapper.or().eq("出版日期",item);
                }
            });
        }

        //  queryWrapper.like("classcode",1);
        queryWrapper.eq(StringUtils.isNotBlank(year),"出版日期", year);
        queryWrapper.eq("SYS_FLD_CHECK_STATE",1);
        queryWrapper.eq(StringUtils.isNotBlank(entity.get丛书名()),"丛书名",entity.get丛书名());
        queryWrapper.like(StringUtils.isNotBlank(bookname),serchtype, bookname);
        queryWrapper.orderByDesc("downnum","previewnum","bb");
        //IPage<BookEntity> ipage = this.page(new Query<BookEntity>(page).getPage(),queryWrapper);
        IPage<BookEntity> ipage =this.baseMapper.queryPage1 (new Query<BookEntity>(page).getPage(),queryWrapper);
        for(int i=0;i<ipage.getRecords().size();i++){
            BookEntity bookEntity=ipage.getRecords().get(i);
            String   classcode=bookEntity.getClasscode();
            if(classcode.indexOf(",")>=0){
                String[] classcodes=classcode.split(",");
                String tempclassname="";
                for(int j=0;j<classcodes.length;j++){
                    BookSortEntity  tt=new  BookSortEntity();
                    tt.setCode(classcodes[j]);
                    tt=bookClsService.querybookclsbycode(tt);
                    if(tt!=null){
                        tempclassname+=tt.name+",";}
                }
                tempclassname=tempclassname.substring(0,tempclassname.lastIndexOf(","));
                bookEntity.setClassname(tempclassname);
            }
            else {
                BookSortEntity  tt=new  BookSortEntity();
                tt.setCode(classcode);
                tt=bookClsService.querybookclsbycode(tt);
                if(tt!=null) {
                    bookEntity.setClassname(tt.getName());
                }
            }
        }
        return new PageUtils(ipage);
    }


    @Override
    public List<BookEntity> querygroup(String name,String classcode) {
        List<BookEntity> nb=new  ArrayList<BookEntity>();
        QueryWrapper<BookEntity>  ss=new QueryWrapper<BookEntity>().select(name,"count(0) as count")
                .isNotNull(name)
                .groupBy(name).eq("SYS_FLD_CHECK_STATE",1)
                .likeRight(StringUtils.isNotBlank(classcode),"classcode",classcode);
        nb= this.list(ss);

        return nb;
    }



    @Override
    public PageUtils findListPage(BookEntity user, PageUtils page) {
        Page<BookEntity> page2 = new Page<>(page.getCurrPage(),page.getPageSize());
        IPage<BookEntity> ipage= this.baseMapper.findListPage(page2,user);
        return new PageUtils(ipage);
    }




}
